﻿using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System;
using CommomFunctions;

namespace Árvore_Binária_de_Pesquisa
{
    /// <summary>
    /// Interaction logic for BinarySearchTree.xaml
    /// </summary>
    public partial class BinarySearchTree : Window
    {
        /// <summary>
        /// Variable that contains the information about the controls and their operations.
        /// </summary>        
        private ControlVariables ctrls = new ControlVariables();

        /// <summary>
        /// Constructor that executes the default initialization and then creates the other necessary controls 
        /// </summary>
        public BinarySearchTree()
        {
            InitializeComponent();
            createBSTree();
        }

        /// <summary>
        /// Creates the Binary Search Tree. 
        /// The array "btns" contains all the buttons in the project. It will be used to control
        /// which button is enabled or disabled according to the operation that is being executed.
        /// </summary>
        public void createBSTree()
        {
            Button[] btn = { btnComplete, btnCreate, btnDestroy, btnInsert, btnRemove, btnSearch };
            ctrls.btnsToEnableDisable(btn);
            MenuItem[] menus = { preOrderLeft, preOrderRight, inOrderLeft, inOrderRight, postOrderLeft, postOrderRight };
            ctrls.menusToEnableDisable(menus);
            ctrls.addOtherControls(Gridi, txtInput, ctrls);
        }
        
        private void grid_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            ctrls.gridSizeChanged(ctrls);
        }
        
        #region Menus click events controls

        #region Traversals menu
        private void preOrderRight_Click(object sender, RoutedEventArgs e)
        {
            ctrls.menuClicked(TraversalClicked.preOrderRight, ctrls);
        }

        private void inOrderRight_Click(object sender, RoutedEventArgs e)
        {
            ctrls.menuClicked(TraversalClicked.inOrderRight, ctrls);
        }

        private void postOrderRight_Click(object sender, RoutedEventArgs e)
        {
            ctrls.menuClicked(TraversalClicked.postOrderRight, ctrls);
        }

        private void preOrderLeft_Click(object sender, RoutedEventArgs e)
        {
            ctrls.menuClicked(TraversalClicked.preOrderLeft, ctrls);
        }

        private void inOrderLeft_Click(object sender, RoutedEventArgs e)
        {
            ctrls.menuClicked(TraversalClicked.inOrderLeft, ctrls);
        }

        private void postOrderLeft_Click(object sender, RoutedEventArgs e)
        {
            ctrls.menuClicked(TraversalClicked.postOrderLeft, ctrls);
        }

        #endregion

        #region Which node to remove? menu
        private void removePredecessor_Click(object sender, RoutedEventArgs e)
        {
            ctrls.menuClicked(MenuClicked.removePredecessor, removePredecessor, removeSuccessor);
        }

        private void removeSuccessor_Click(object sender, RoutedEventArgs e)
        {
            ctrls.menuClicked(MenuClicked.removeSuccessor, removePredecessor, removeSuccessor);
        }
        #endregion

        #region Help menu
        private void about_Click(object sender, RoutedEventArgs e)
        {
            Ajuda.openAbout();
        }

        private void help_Click(object sender, RoutedEventArgs e)
        {
            Ajuda.openHelp();
        }

        private void tutorial_Click(object sender, RoutedEventArgs e)
        {
            Ajuda.openTutorial("Árvore Binária.wmv");
        }
        #endregion
        
        #endregion

        #region Buttons click events controls
       
        #region Operation buttons
        private void btnInsere_Click(object sender, RoutedEventArgs e)
        {
            ctrls.buttonClicked(ButtonClicked.Insert, txtInput, ctrls);
        }
       
        private void btnRemove_Click(object sender, RoutedEventArgs e)
        {
            ctrls.buttonClicked(ButtonClicked.Remove, txtInput, ctrls);
        }

        private void btnProcura_Click(object sender, RoutedEventArgs e)
        {
            ctrls.buttonClicked(ButtonClicked.Search, txtInput, ctrls);
        }
        #endregion
               
        #region Creation buttons
        private void btnExhibitComplete_Click(object sender, RoutedEventArgs e)
        {
            ctrls.writeTrace("\nExibindo árvore completa", Brushes.Orchid);
            ctrls.buttonClicked(ButtonClicked.Complete, ctrls);
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            ctrls.writeTrace("\nCria nova árvore vazia", Brushes.RoyalBlue);
            ctrls.buttonClicked(ButtonClicked.Create, ctrls);
        }

        private void btnDestroy_Click(object sender, RoutedEventArgs e)
        {
            ctrls.writeTrace("\nDestrói árvore", Brushes.RoyalBlue);
            ctrls.buttonClicked(ButtonClicked.Destroy, ctrls);
        }  
        #endregion

        #endregion

        
    }
}
